package com.yike.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yike.common.exception.BusinessException;
import com.yike.common.tool.LoginUtil;
import com.yike.product.entity.GoodsSpecification;
import com.yike.product.mapper.GoodsSpecificationMapper;
import com.yike.product.param.GoodsSpecificationParam;
import com.yike.product.service.GoodsSpecificationService;
import com.yike.product.param.GoodsSpecificationPageParam;
import com.yike.common.base.BaseServiceImpl;
import com.yike.common.pagination.Paging;
import com.yike.common.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.product.vo.GoodsSpecificationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 商品规格 服务实现类
 *
 * @author Sakura
 * @since 2024-10-18
 */
@Slf4j
@Service
public class GoodsSpecificationServiceImpl extends BaseServiceImpl<GoodsSpecificationMapper, GoodsSpecification> implements GoodsSpecificationService {

    @Autowired
    private GoodsSpecificationMapper goodsSpecificationMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveGoodsSpecification(GoodsSpecificationParam goodsSpecificationParam) throws Exception {
        // 先判断是否存在重复名称
        GoodsSpecification goodsSpecification = goodsSpecificationMapper.selectOne(Wrappers.lambdaQuery(GoodsSpecification.class)
                .eq(GoodsSpecification::getName, goodsSpecificationParam.getName()));
        if (goodsSpecification != null) {
            throw new BusinessException("当前规格已存在，请勿重复添加");
        }
        // 保存
        goodsSpecification = new GoodsSpecification();
        BeanUtils.copyProperties(goodsSpecificationParam, goodsSpecification);
        goodsSpecification.setCreateBy(LoginUtil.getUserId());
        return super.save(goodsSpecification);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateGoodsSpecification(GoodsSpecificationParam goodsSpecificationParam) throws Exception {
        // 先判断信息是否存在
        GoodsSpecification goodsSpecification = goodsSpecificationMapper.selectById(goodsSpecificationParam.getId());
        if (goodsSpecification == null) {
            throw new BusinessException("规格信息异常");
        }
        // 校验修改时是否出现重名
        Integer checkNum = goodsSpecificationMapper.selectCount(Wrappers.lambdaQuery(GoodsSpecification.class)
                .eq(GoodsSpecification::getName, goodsSpecificationParam.getName())
                .ne(GoodsSpecification::getId, goodsSpecificationParam.getId()));
        if (checkNum > 0) {
            throw new BusinessException("当前规格已存在，请勿重复添加");
        }
        BeanUtils.copyProperties(goodsSpecificationParam, goodsSpecification);
        goodsSpecification.setUpdateBy(LoginUtil.getUserId());
        return super.updateById(goodsSpecification);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteGoodsSpecification(Long id) throws Exception {
        return super.removeById(id);
    }

    @Override
    public GoodsSpecificationVo getGoodsSpecification(Long id) throws Exception {
        return goodsSpecificationMapper.getGoodsSpecificationVo(id);
    }

    @Override
    public Paging<GoodsSpecificationVo> getGoodsSpecificationPageList(GoodsSpecificationPageParam goodsSpecificationPageParam) throws Exception {
        Page<GoodsSpecification> page = new PageInfo<>(goodsSpecificationPageParam);
        IPage<GoodsSpecificationVo> iPage = goodsSpecificationMapper.getGoodsSpecificationList(page, goodsSpecificationPageParam);
        return new Paging<GoodsSpecificationVo>(iPage);
    }

}
